package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.entity.Order;
import com.example.exception.FloraException;
import com.example.mapper.OrderMapper;
import com.example.service.OrderItemService;
import com.example.service.OrderService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private ProductService productService;

    @Override
    public IPage<Order> getOrder(Long orderId, int pageNum, int pageSize) {
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        Page<Order> page = new Page<>(pageNum,pageSize);
        qw.eq(Order::getOrderId,orderId);
        return page(page,qw);
    }


    /*@Override
    public ResponseResult getAllOrderList(Integer orderId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Order> orderWrapper = new LambdaQueryWrapper<>();
        orderWrapper.eq(Order::getStatus,SystemConstants.ORDER_IS_COMPLETED);
        Page<Order> page = new Page<>(pageNum,pageSize);
        page(page,orderWrapper);
        List<OrderVo> orderVos = BeanCopyUtils.copyBeanList(page.getRecords(), OrderVo.class);

        for(OrderVo orderVo:orderVos){
            orderVo.setChildren(getChildren(Long.valueOf(orderVo.getOid())));
        }
        return ResponseResult.okResult(new PageVo(orderVos,page.getTotal()));
    }*/

    /*public List<OrderItemVo> getChildren(Long orderId){
        LambdaQueryWrapper<OrderItem> qw=new LambdaQueryWrapper<>();
        qw.eq(OrderItem::getOid,orderId);
        List<OrderItem> orderItems=orderItemService.list(qw);
        List<OrderItemVo> orderItemVos = BeanCopyUtils.copyBeanList(orderItems, OrderItemVo.class);
        return orderItemVos;
    }   */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Order addOrder(Order order) throws FloraException {
        try {
            this.baseMapper.insert(order);
            return order;
        } catch (Exception e) {
            throw new FloraException("添加订单失败"+e);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOrder(Long orderId) throws FloraException {
        /** 先查看该订单是否存在 */
        Order order = this.baseMapper.selectById(orderId);
        if(order==null){
            throw new FloraException("该订单不存在");
        }
        try{
            this.baseMapper.deleteById(orderId);
        } catch (Exception e) {
            throw new FloraException("删除该条订单失败");
        }
    }


}
